<template>
  <div>
    <!-- 电商订单更换经销商-->
    <TopFilter :list="topFilterList" :cols="3" @filterChange="changeHandle" @onCollapse="collapseHandle" :labelWidth="110" @resetChange="resetChange"></TopFilter>

    <FilterTable ref="table" columnsRef="qjhEOrderTurnDealer" :columns="columns" :fetchapi="fetchApi" :params="fetchParams" :datakey="'records'" :onColumnsChange="columns => (this.columns = columns)">
      <template slot="controls" slot-scope="props">
        <!-- <multiuse-button size="small" type="primary" @click="addHandle(3)">导 出</multiuse-button> -->
      </template>
    </FilterTable>

    <BaseDialog :visible.sync="isDealer" :title="'经销商查询'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <ownerSeacher :selectType="handleType" :rowData="formData" @close="emitCloseHandler" />
    </BaseDialog>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { getRegion, vehicleConfiguration } from '@/api/omFinance';
import { queryList, changeDealer, getNotPage } from '@/api/omSale/OrderManagement/EOrderTurnDealer';
import ownerSeacher from './ownerSeacher';

export default {
  name: 'EOrderTurnDealer',
  mixins: [dictionary],

  data() {
    this.BaseTable = null;
    return {
      topFilterList: this.createdFrom(),
      columns: this.createdTable(),
      fetchApi: queryList,
      fetchParams: {},
      formData: {},
      handleType: '',
      rowId: '',
      isDealer: false
    };
  },
  components: {
    ownerSeacher
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  created() {
    this.getvehicleList();
  },
  methods: {
    async getvehicleList() {
      let res = await vehicleConfiguration(0, { isUnlimited: 10041001 });
      this.topFilterList.find(item => item.fieldName === 'brandProductList').itemList = res.data;
    },
    async getNotPage(orderType) {
      let list = await getNotPage({ orderType: orderType });
      this.topFilterList.map(x => {
        if (x.fieldName == 'orderDetailType') {
          x.itemList = list.data.map(x => ({ text: x.subName, value: x.subId }));
        }
      });
    },
    async changeDealer(code) {
      let params = {
        orderId: this.rowId,
        dealerCode: code
      };
      this.BaseTable.START_LOADING();

      const res = await changeDealer(params);
      if (res.resultCode == 200) {
        if (res.resultCode == 200) {
          this.BaseTable.STOP_LOADING();

          this.$notify.success({ title: '提示信息', message: '更换成功' });
          this.fetchParams = Object.assign({}, this.fetchParams);
        }
      } else {
        this.BaseTable.STOP_LOADING();
      }
    },

    changeHandle(params) {
      this.fetchParams = Object.assign({}, this.fetchParams, params);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    resetChange() {
      this.fetchParams = Object.assign({}, this.fetchParams);
      this.topFilterList.map(x => {
        if (x.fieldName == 'officeId') {
          x.itemList = [];
        }
        if (x.fieldName == 'orderDetailType') {
          x.itemList = [];
        }
      });
    },
    seacherFun() {
      this.isDealer = true;
      this.handleType = 'multiple';
    },
    emitCloseHandler(val, data, type) {
      console.log(type, data, '---奥利给---');
      this.isDealer = val;
      if (data && type == 'multiple') {
        let dealerCodes = [];
        let dealerNames = [];
        data.map(x => {
          dealerCodes.push(x.dealerCode);
          dealerNames.push(x.dealerName);
        });
        this.topFilterList.find(x => x.fieldName == 'dealerCodes').initialValue = dealerCodes.toString();
        return;
      }
      if (data && type == 'single') {
        this.changeDealer(data[0].dealerCode);
      }
    },

    editHandle(row) {
      console.log(row, '---奥利给---');
      this.isDealer = true;
      this.rowId = row.orderId;
      this.handleType = 'single';
      this.formData = row;
    },

    createdFrom() {
      return [
        {
          type: 'INPUT',
          label: '经销商代码',
          fieldName: 'dealerCodes',
          placeholder: '请选择',
          unitRender: () => {
            return (
              <div>
                <el-button icon="el-icon-search" onClick={() => this.seacherFun()}></el-button>
              </div>
            );
          }
        },
        {
          type: 'INPUT',
          label: '经销商名称',
          fieldName: 'dealerName',
          placeholder: '请输入'
        },
        {
          type: 'SELECT',
          label: '订单类型',
          fieldName: 'orderType',
          placeholder: '请选择',
          itemList: this.createDictList('6006'),
          change: p => {
            if (p) {
              this.getNotPage(p);
            } else {
              this.topFilterList.find(x => x.fieldName == 'orderDetailType').itemList = [];
              this.topFilterList.find(x => x.fieldName == 'orderDetailType').initialValue = '';
            }
          }
        },
        {
          type: 'SELECT',
          label: '订单类型细分',
          fieldName: 'orderDetailType',
          placeholder: '请选择',
          itemList: []
        },

        {
          type: 'RANGE_DATE',
          label: '提报日期',
          fieldName: 'reportDateBegin|reportDateEnd',
          placeholder: '请选择',
          initialValue: [],
          dateType: 'exactdaterange'
        },
        {
          type: 'INPUT_CASCADER',
          label: '车型选择',
          fieldName: 'brandProductList',
          placeholder: '请选择',
          options: { titles: ['品牌', '车系', '车型', '配置', '选装', '颜色'] },
          itemList: []
        },
        {
          type: 'INPUT',
          label: '订货单编号',
          fieldName: 'orderNo',
          placeholder: '请选择'
        },
        {
          type: 'INPUT',
          label: '销售订单编号',
          fieldName: 'doNo',
          placeholder: '请选择'
        }
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 50,
          fixed: 'left',
          render: (props, h) => {
            return (
              <div>
                <multiuse-button size="mini" type="text" onClick={() => this.editHandle(props.row)}>
                  更换
                </multiuse-button>
              </div>
            );
          }
        },
        {
          title: '事业部',
          dataIndex: 'deptName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '办事处',
          dataIndex: 'officeName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '经销商代码',
          dataIndex: 'dealerCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '经销商名称',
          dataIndex: 'dealerName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '订货单编号',
          dataIndex: 'orderNo',
          sorter: true,
          filter: true,
          filterType: 'input',
          align: 'center',
          width: 200
        },
        {
          title: '销售订单编号',
          dataIndex: 'doNo',
          sorter: true,
          filter: true,
          filterType: 'input',
          align: 'center',
          width: 200
        },
        {
          title: '订单类型',
          dataIndex: 'orderType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6006'),
          render: p => {
            return <span>{this.createDictText(p.row.orderType, '6006')}</span>;
          }
        },
        {
          title: '订单细分类型',
          dataIndex: 'orderDetailTypeName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },

        {
          title: '订货单状态',
          dataIndex: 'orderStatus',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6043'),
          render: p => {
            return <span>{this.createDictText(p.row.orderStatus, '6043')}</span>;
          }
        },
        {
          title: '销售订单状态',
          dataIndex: 'deliveryOrderStatus',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6002'),
          render: p => {
            return <span>{this.createDictText(p.row.deliveryOrderStatus, '6002')}</span>;
          }
        },
        {
          title: '产品代码',
          dataIndex: 'productCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '产品名称',
          dataIndex: 'productName',
          align: 'center',
          width: 400,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '车系',
          dataIndex: 'seriesName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '车型',
          dataIndex: 'modelName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '配置',
          dataIndex: 'packageName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '选装',
          dataIndex: 'optionName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '内饰',
          dataIndex: 'trimName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '外饰',
          dataIndex: 'colorName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '提报数量',
          dataIndex: 'orderNum',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '提报时间',
          dataIndex: 'reportDate',
          sorter: true,
          filter: true,
          filterType: 'date-range',
          dateFormat: 'yyyy-MM-dd'
        }
      ];
    }
  }
};
</script>
